package android.Spider;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.content.Context;

public class PrivateMessages
{
	public class Message
	{
		public boolean m_bRead;
		public int m_idMsg;
		public String m_strFrom;
		public String m_strMsg;
	}
	
	private static String s_strFileName = "PrivateMessges.dat";
	
	private PrivateMessages(Context ctx)
	{
		m_ctx = ctx;
		// Read from the file.
		try
		{
			FileInputStream stream = ctx.openFileInput(s_strFileName);
			ObjectInputStream istream = new ObjectInputStream(stream);
			
			int nSize = istream.readInt();
			m_lstMessages = new ArrayList<Message>(nSize);
			for (int i = 0; i < nSize; ++i)
			{
				Message m = new Message();
				
				m.m_bRead = istream.readBoolean();
				m.m_idMsg = istream.readInt();
				s_idLast = Math.max(s_idLast, m.m_idMsg);
				m.m_strFrom = (String)istream.readObject();
				m.m_strMsg = (String)istream.readObject();

				m_lstMessages.add(m);
			}
		}
		catch (FileNotFoundException e) {
			System.out.println(e.toString());
		}
		catch (EOFException e) {
			System.out.println(e.toString());
		}
		catch (ClassNotFoundException e) {
			System.out.println(e.toString());
		}
		catch (IOException e) {
			System.out.println(e.toString());
		}
	}
	
	public void Store()
	{
		synchronized (this)
		{
			try {
				m_ctx.deleteFile(s_strFileName);
				FileOutputStream stream = m_ctx.openFileOutput(s_strFileName, Context.MODE_APPEND);
				ObjectOutputStream ostream = new ObjectOutputStream(stream);
				
				int nSize = m_lstMessages.size();
				ostream.writeInt(nSize);
				for (int i = 0; i < nSize; ++i)
				{
					Message msg = m_lstMessages.get(i);

					ostream.writeBoolean(msg.m_bRead);
					ostream.writeInt(msg.m_idMsg);
					ostream.writeObject(msg.m_strFrom);
					ostream.writeObject(msg.m_strMsg);
				}

				ostream.close();
				stream.close();
			}
			catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			catch (NotSerializableException e) {
				e.printStackTrace();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static PrivateMessages Instance(Context ctx)
	{
		if (s_this == null)
			s_this = new PrivateMessages(ctx);
		
		return s_this;
	}
	
	public int AddMessage(String strFrom, String strMsg)
	{
		Message msg = new Message();
		msg.m_bRead = false;
		msg.m_idMsg = ++s_idLast;
		msg.m_strFrom = strFrom;
		msg.m_strMsg = strMsg;
		m_lstMessages.add(msg);
		
		return msg.m_idMsg;
	}

	public int GetMessagesCount() { return m_lstMessages.size(); }
	
	public Message GetMessage(int idMessage)
	{
		Message msg = null;
		for (int i = 0; i < m_lstMessages.size() && msg == null; ++i)
		{
			if (m_lstMessages.get(i).m_idMsg == idMessage)
				msg = m_lstMessages.get(i);
		}
		
		return msg;
	}
	
	public Message GetMessageByPos(int nPos)
	{
		return m_lstMessages.get(nPos);
	}
	
	public void DeleteMessageByPos(int nPos)
	{
		m_lstMessages.remove(nPos);
	}
	
	public void Clear()
	{
		m_lstMessages.clear();
	}
	
	public int GetUnreadCount()
	{
		int nCount = 0;
		for (int i = 0; i < m_lstMessages.size(); ++i)
		{
			if (!m_lstMessages.get(i).m_bRead)
				++nCount;
		}
		
		return nCount;
	}
	
	public ArrayList<Message> GetAllMessages()
	{
		return m_lstMessages;
	}
	
	private static PrivateMessages s_this = null;
	private static int s_idLast = 0;
	
	private ArrayList<Message> m_lstMessages = new ArrayList<Message>();
	private Context m_ctx;

}
